<?php

namespace App\Models\Faculty;

use App\Models\System\Term;
use App\Models\System\User;
use Illuminate\Database\Eloquent\Model;

class CreditTraining extends Model
{
    protected $table = 'credit_training';
    protected $guarded = [];
    protected $hidden = ['created_at','updated_at'];
    protected $appends = [
        'course_level_set',
        'type_set'
    ];

    public function term()
    {
        return $this->hasOne(Term::class, 'id', 'data_version_id');
    } 

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    } 
    
    public function getCourseLevelSetAttribute(){
        $courseLevel = !empty($this->attributes['course_level']) ? $this->attributes['course_level'] : '';

        if(!$courseLevel){
            return [];
        }

        return json_decode($courseLevel, true);
    }
    
    public function getTypeSetAttribute(){
        $type = !empty($this->attributes['type']) ? $this->attributes['type'] : '';

        if($type){
            switch($type) {
                case 'CITY':
                    return '市';
                    break;
                case 'DISTRICT':
                    return '校';
                    break;
            }
        }

        return '';
    }
}
